package com.meizi.codegenerate.utils;

import com.meizi.codegenerate.entity.ColumnsEntity;
import com.meizi.codegenerate.enums.ColumnToImportEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administration
 * @date 2022/12/23 - 16:23 - 星期五
 * @project lambda
 * @package com.meizi.codegenerate.utils
 * @by 晨
 */
public class ColumnUtils {

    public static final String LEFT_BRACKETS = "(";

    public static final String RIGHT_BRACKETS = ")";


    /**
     * 根据查出来数据表的字段类型模糊匹配对应的import
     * @param columnsEntities 传入字段类型集合
     * @return
     */
    public static HashMap<String,String> mapColumnDateType (List<ColumnsEntity> columnsEntities) {
        HashMap<String,String> hashMap = new HashMap<>();
        if (CollectionUtils.isEmpty(columnsEntities)) {
            return hashMap;
        }

        // 是否包含对应的类型  varchar(512) 从0开始截取最后（最后一次出现前所有的字母不包含
        List<ColumnsEntity> columnLists = columnDateType(columnsEntities);
        List<String> columnList = columnLists.stream().map(ColumnsEntity::getType).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(columnList)) {
            for (String columnType : columnList) {
                if (ColumnToImportEnum.containsValue(columnType)) {
                    hashMap.put(ColumnToImportEnum.from(columnType).getValue(),
                            ColumnToImportEnum.from(columnType).getValue());
                }
            }
        }

        return hashMap;
    }

    /**
     * 是否包含对应的类型  varchar(512) 从0开始截取最后（最后一次出现前所有的字母不包含
     * @param columnsEntities
     * @return
     */
    public static List<ColumnsEntity> columnDateType(List<ColumnsEntity> columnsEntities) {
        List<ColumnsEntity> columnList = new ArrayList<>();
        for (ColumnsEntity columnsEntity : columnsEntities) {
            if (StringUtils.isNotBlank(columnsEntity.getType())) {
                String substring;
                if (columnsEntity.getType().contains(LEFT_BRACKETS) &&
                        columnsEntity.getType().contains(RIGHT_BRACKETS)) {
                    substring = columnsEntity.getType()
                            .substring(0, columnsEntity.getType()
                                    .lastIndexOf(LEFT_BRACKETS));
                } else {
                    substring = columnsEntity.getType();
                }
                columnsEntity.setType(substring);
                columnList.add(columnsEntity);
            }
        }
        return columnList;
    }

    /**
     * 数据库字段类型转换java类型
     * @param afferentHashMap 传入参数
     * @return
     */
    public static HashMap<String,String> columnTypeToImport (HashMap<String, String> afferentHashMap) {
        HashMap<String,String> returnMap = new HashMap<>();
        if (afferentHashMap.isEmpty()) {
            returnMap.put("null","null");
            return returnMap;
        }

        //返回对应的import
        toImport(afferentHashMap, returnMap);

        return returnMap;
    }

    /**
     * 数据库字段类型转换成对应java对应字段类型
     * @param afferentHashMap 传入参数
     * @param returnMap 返回参数
     */
    private static void toImport(HashMap<String, String> afferentHashMap, HashMap<String, String> returnMap) {
        for (String key : afferentHashMap.keySet()) {
            if (ColumnToImportEnum.containsValue(key)) {
                returnMap.put(ColumnToImportEnum.from(key).getValue(), ColumnToImportEnum.from(key).getDesc());
            }
        }
    }
}
